package com.daviteq.ems.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.TimeZone;

/**
 * @author <a href="mailto:thitranthanh@gmail.com">thitranthanh</a>
 * @date Apr 26, 2013
 * @copyright Dai Viet Controls & Instrumentation Company
 */
public class DateTime {

    private String TIME_ZONE = "GMT+7";

    private DateTime() {
    }

    private static class Loader {

        private static final DateTime INSTANCE = new DateTime();
    }

    public static DateTime getInstance() {
        return Loader.INSTANCE;
    }

    public String getLocalTimeZone() {
        return TIME_ZONE;
    }

    public void setLocalTimeZone(String localTimeZone) {
        this.TIME_ZONE = localTimeZone;
    }

    public String getDateTime(String format, String timezone) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(timezone));
        return simpleDateFormat.format(new java.util.Date());
    }

    public String getDateTime(String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
        return simpleDateFormat.format(new java.util.Date());
    }

    public String getDateTime(long timestamp, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
        return simpleDateFormat.format(timestamp);
    }

    public String getTime(long timestamp, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
        return simpleDateFormat.format(timestamp);
    }

    public String getDate(long timestamp, String format) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format);
        simpleDateFormat.setTimeZone(TimeZone.getTimeZone(TIME_ZONE));
        return simpleDateFormat.format(timestamp);
    }

    /* Begin get timestamp */
    /*public byte[] getTimeStamp() {
        return longToBytes(System.currentTimeMillis(), 8);
    }
    
    public static byte[] longToBytes(long l, int size) {
        return ByteBuffer.allocate(size).putLong(l).array();
    }*/
    /* End get timestamp */

    public String getEpochTimestamp(String format) {
        return getDateTime(System.currentTimeMillis(), format);
    }

    public long getTimestamp(String timestamp, String formatDateTime) {
        try {
            SimpleDateFormat sdf =
                    new SimpleDateFormat(formatDateTime);
            return sdf.parse(timestamp).getTime();
        } catch (ParseException e) {
            return -1;
        }
    }

    public String convertDateToDate(String date, String oldFDate,
            String newFdate) {
        long time = getTimestamp(date, oldFDate);
        return getDateTime(time, newFdate);
    }

    public Date convertStringToDate(String date, String format)
            throws ParseException {
        SimpleDateFormat df = new SimpleDateFormat(format);
        try {
            return df.parse(date);
        } catch (Exception ex) {
            throw new RuntimeException(ex);
        }
    }

    public int getDayNumberOfWeek(String dateTime, String format)
            throws ParseException {

        SimpleDateFormat df = new SimpleDateFormat(format);
        Date date = df.parse(dateTime);
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);

        if (Calendar.SUNDAY == cal.get(Calendar.DAY_OF_WEEK)) {
            return 7;
        }

        return (cal.get(Calendar.DAY_OF_WEEK) - 1);
    }

    public static void main(String[] args) throws ParseException {

        String timestamp = String.valueOf(System.currentTimeMillis());
        System.out.println(timestamp);

        DateTime dt = DateTime.getInstance();
        String l = dt.getDateTime("D");
        System.out.println(l + "");

        // String input = "20131006";
        // String format = "yyyyMMdd";
        //
        // SimpleDateFormat df = new SimpleDateFormat(format);
        // Date date = df.parse(input);
        //
        // Calendar cal = Calendar.getInstance();
        // cal.setTime(date);
        // int week = cal.get(Calendar.DAY_OF_WEEK);
        //
        // System.out.println(week-1);
        //
        // String s = dt.getDateTime(System.currentTimeMillis());
        //
        // long time = dt.getTimestamp("20130927120000", "yyyyMMddHHmmss");
        //
        // String rs = dt.getDateTime(time, "mm");
        //
        // System.out.println(time + " --> " + rs);

    }

}
